package com.luck.easyfoodbatch.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMCmdMessageBody;
import com.hyphenate.chat.EMMessage;
import com.luck.easyfoodbatch.DemoHelper;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.db.MessageDB;
import com.luck.easyfoodbatch.fragment.ChatMessageFragment;
import com.luck.easyfoodbatch.fragment.SystemMessageFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MessageActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.left_back)
    ImageButton leftBack;
    @BindView(R.id.rab1)
    RadioButton rab1;
    @BindView(R.id.rab2)
    RadioButton rab2;
    @BindView(R.id.rabs)
    RadioGroup rabs;
    @BindView(R.id.vp)
    ViewPager vp;
    @BindView(R.id.tv_dot)
    TextView tv_dot;
    private int currIndex = 0;// 当前页卡编号
    private List<Fragment> fragments = new ArrayList<Fragment>();
    private ChatMessageFragment chatFragment;
    private SystemMessageFragment systemFragment;
    private MessageDB messageDB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        ButterKnife.bind(this);
        messageDB = new MessageDB(this);
        currIndex = getIntent().getIntExtra("currIndex", 0);
        chatFragment = new ChatMessageFragment();
        systemFragment = new SystemMessageFragment();
        fragments.add(chatFragment);
        fragments.add(systemFragment);
        rabs.setOnCheckedChangeListener(this);
        vp.addOnPageChangeListener(new MyOnPageChangeListener());
        vp.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments));
        vp.setCurrentItem(currIndex);

        EMClient.getInstance().chatManager().addMessageListener(messageListener);
    }

    @OnClick(R.id.left_back)
    public void onClick() {
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initDot();
    }

    /**
     * 设置红点是否显示
     */
    public void initDot() {
        if (isCheckedLogin()) {
            // 如果当前tab页是在系统消息中，不显示右上角红点
            int currentItem = vp.getCurrentItem();
            if (currentItem != 1) {
                int messagesCount = messageDB.getByAllMessagesCount();
                if (messagesCount > 0) {
                    tv_dot.setVisibility(View.VISIBLE);
                } else {
                    tv_dot.setVisibility(View.GONE);
                }
            }
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.rab1:
                currIndex = 0;
                break;
            case R.id.rab2:
                currIndex = 1;
                break;
        }
        vp.setCurrentItem(currIndex);
        int currentItem = vp.getCurrentItem();
        if (currentItem != 1) {
            initDot();
        } else {
            tv_dot.setVisibility(View.GONE);
        }
    }


    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageSelected(int arg0) {
            currIndex = arg0;
            switch (arg0) {
                case 0:
                    rab1.setChecked(true);
                    break;
                case 1:
                    rab2.setChecked(true);
                    break;
            }
            vp.setCurrentItem(currIndex);
            if (currIndex != 1) {
                initDot();
            } else {
                tv_dot.setVisibility(View.GONE);
            }
        }
    }

    public class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {

        private List<Fragment> list;

        public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
            super(fm);
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Fragment getItem(int arg0) {
            return list.get(arg0);
        }

    }

    EMMessageListener messageListener = new EMMessageListener() {

        @Override
        public void onMessageReceived(List<EMMessage> messages) {
            // notify new message
            for (EMMessage message : messages) {
                DemoHelper.getInstance().getNotifier().onNewMsg(message);
            }
            refreshUIWithMessage();
        }

        @Override
        public void onCmdMessageReceived(List<EMMessage> messages) {
            //end of red packet code
            refreshUIWithMessage();
        }

        @Override
        public void onMessageReadAckReceived(List<EMMessage> messages) {
        }

        @Override
        public void onMessageDeliveryAckReceived(List<EMMessage> message) {
        }

        @Override
        public void onMessageChanged(EMMessage message, Object change) {
        }
    };

    /**
     * 监听新消息
     */
    private void refreshUIWithMessage() {
        runOnUiThread(new Runnable() {
            public void run() {
                if (chatFragment != null) {
                    chatFragment.refresh();
                }
            }
        });
    }
}
